<?php


namespace app\admin\controller\content;

use app\admin\controller\Base;


use app\common\model\Video as models;
use app\common\model\VideoToVip;
use app\common\traites\PublicCrudTrait;
use think\exception\ValidateException;
use \app\validate\Video as validates;
use think\facade\Db;

class Video extends Base
{
    protected $model;
    use PublicCrudTrait;

    /**
     * 视频控制器
     */
    public function initialize()
    {
        $this->model = new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 列表
     */
    public function index()
    {
        //当前页
        $page = $this->request->post('page', 1);
        //每页显示数量
        $limit = $this->request->post('limit', 10);
        //查询搜索条件
        $post = array_filter($this->request->post());
        $map = [];
        isset($post['title']) && $map [] = ['a.title', 'like', '%' . $post['title'] . '%'];

        $list = $this->model->page_list($map, $limit, $page);
        return $this->success($list);
    }

    /**
     * 添加
     */
    public function add()
    {
        //过滤数据
        $postField = 'type,title,des,sort,status,thumb_url,video_url,description,video_price';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('add')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }

        $user = $this->model->where('title', $post['title'])->find();
        if ($user) return $this->failed('该标题以存在');

        isset($post['thumb_url']) && $post['thumb_url'] = image_update($post['thumb_url']);
        isset($post['video_url']) && $post['video_url'] = image_update($post['video_url']);
        !isset($post['thumb_url']) && $post['thumb_url'] = '';;

        //添加的时候顺便插入到 video_to_vip 表一条空数据
        $save = false;
        Db::startTrans();
        try {
            $this->model->save($post);
            (new VideoToVip())->insert(['video_id' => $this->model->id]);
            $save = true;
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }

        if ($save) return $this->success([]);
        return $this->failed('新增失败');
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        //过滤数据
        $postField = 'id,type,title,thumb_url,des,sort,status,video_url,description,video_price';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('edit')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->failed($e->getError());
        }

        //查询是否重复的该菜单
        $find = $this->model->where('title', $post['title'])->where('id', '<>', $post['id'])->find();
        if ($find) return $this->failed('该标题以存在');

        if (isset($post['thumb_url']) && !empty($post['thumb_url'])) {
            $post['thumb_url'] = image_update($post['thumb_url']);
        }
        if (isset($post['video_url']) && !empty($post['video_url'])) {
            $post['video_url'] = image_update($post['video_url']);
        }

        !isset($post['video_url']) && $post['video_url'] = '';
        !isset($post['thumb_url']) && $post['thumb_url'] = '';
        //执行修改数据
        $save = $this->model->update($post);
        if ($save) return $this->success([]);
        return $this->failed('修改失败');
    }
}